/**
 * 14位日期格式化
 * @param dateTimeString
 * @returns
 */
const formatDateTime = (dateTimeString: string): string => {

  if (typeof dateTimeString !== "string" || !/^\d{14}$/.test(dateTimeString)) {
    return dateTimeString;
  }


  const year = dateTimeString.slice(0, 4);
  const month = dateTimeString.slice(4, 6);
  const day = dateTimeString.slice(6, 8);
  const hour = dateTimeString.slice(8, 10);
  const minute = dateTimeString.slice(10, 12);


  return `${year}-${month}-${day} ${hour}:${minute}`;
};
/**
 * 显示最近多久创建
 * @param dateTimeString
 * @returns
 */
const formatDateTimeToNew = (dateTimeString: string): string => {

  if (typeof dateTimeString !== "string" || !/^\d{14}$/.test(dateTimeString)) {
    return dateTimeString;
  }


  const year = parseInt(dateTimeString.slice(0, 4), 10);
  const month = parseInt(dateTimeString.slice(4, 6), 10) - 1;
  const day = parseInt(dateTimeString.slice(6, 8), 10);
  const hour = parseInt(dateTimeString.slice(8, 10), 10);
  const minute = parseInt(dateTimeString.slice(10, 12), 10);

  const inputDate = new Date(year, month, day, hour, minute);
  const currentDate = new Date();


  const timeDifference = currentDate.getTime() - inputDate.getTime();
  const minutesDifference = Math.floor(timeDifference / (1000 * 60));
  const hoursDifference = Math.floor(minutesDifference / 60);
  const daysDifference = Math.floor(hoursDifference / 24);


  if (daysDifference > 1) {

    return `${year}-${String(month + 1).padStart(2, "0")}-${String(
      day
    ).padStart(2, "0")}`;
  } else if (hoursDifference > 0) {

    return `${hoursDifference}小时前`;
  } else {

    return `${minutesDifference}分钟前`;
  }
};
export default { formatDateTime, formatDateTimeToNew };
